//
//  BCLoginVC.m
//  BaoYouYou
//
//  Created by caobo56 on 2016/12/4.
//  Copyright © 2016年 caobo56. All rights reserved.
//

#import "BCLoginVC.h"
#import "AppDelegate.h"
#import "BCTabBarVC.h"
#import "BCRegisterVC.h"

#import "UIView+toast.h"

#import "HyperlinksButton.h"

#import "Live-Header.h"

#import "HTTPRequestManager.h"


@interface BCLoginVC ()
@property (weak, nonatomic) IBOutlet HyperlinksButton *registerBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;


@end

@implementation BCLoginVC

- (IBAction)clearUserName:(id)sender {
    _phoneNumTF.text = @"";
}

- (IBAction)openPasswordSecurity:(id)sender {
    UIButton * btn = (UIButton *)sender;
    if (btn.selected) {
        [btn setImage:[UIImage imageNamed:@"icon_7"] forState:UIControlStateNormal];
        _passwordTF.secureTextEntry = btn.selected = NO;
    }else{
        [btn setImage:[UIImage imageNamed:@"icon_4"] forState:UIControlStateNormal];
        _passwordTF.secureTextEntry = btn.selected = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登  录";
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame= CGRectMake(0, 0, 40, 44);
    [btn addTarget:self action:@selector(selectRightAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"注册" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor  whiteColor] forState:UIControlStateNormal];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightButton;
    [_registerBtn setColor:DEFAULTREDCOLOR];
    UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
    
    [self.view addGestureRecognizer:tapGesture];
    
    // Do any additional setup after loading the view.
}
-(void)Actiondo:(id)sender{
    [self.view endEditing:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)loginBtnPress:(id)sender {
    if (_phoneNumTF.text.length == 0) {
        [self.view toastMessage:@"请输入注册手机号"];
        return;
    }
    
    if (![self isMobileNumber:_phoneNumTF.text]) {
        [self.view toastMessage:@"请输入正确的手机号"];
        return;
    }
    WeakSelf
    [self.view showLoadingWithMessage:nil];
    NSString * url = [NSString stringWithFormat:@"http://183.174.228.55:8089/youbaobao/app/user/loginJson?phoneNo=%@&password=%@",self.phoneNumTF.text,self.passwordTF.text];
    
    [[HTTPRequestManager sharedManager] addGETRequestParameters:url parametes:nil success:^(id successobject) {
        [weakSelf.view hideLodingView];
        if (!successobject || ![successobject isKindOfClass:[NSDictionary class]]) {
            [weakSelf.view toastMessage:@"网络请求错误，请稍后重试"];
            return;
        }
        
        NSDictionary * res = (NSDictionary *)successobject;
        if ([res[@"loginIsOK"] intValue] != 1) {
            [weakSelf.view toastMessage:@"登录失败，请稍后重试"];
            return;
        }else{
            [weakSelf.view toastMessage:@"登录成功" title:@"提示" duration:2.0f image:nil completion:^{
                if ([res[@"loginIsOK"] intValue] == 1) {
                    [weakSelf.view toastMessage:@"登录成功"];
                    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
                    [user setValue:@(1) forKey:@"isLogin"];
                    [user setValue:res[@"phoneNo"] forKey:@"phoneNo"];
                    [user setValue:res[@"password"] forKey:@"password"];
                    [user setValue:res[@"email"] forKey:@"email"];
                    [user setValue:res[@"id"] forKey:@"userid"];
                    [user synchronize];
                }
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }];

        }
        
    } fail:^(id failobject) {
        [weakSelf.view hideLodingView];
        [weakSelf.view toastMessage:@"网络请求错误，请稍后重试"];
    }];
}

-(void)selectRightAction:(id)senser{
    BCRegisterVC * registerVC = (BCRegisterVC *)[[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"BCRegisterVC"];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (IBAction)registerBtnPres:(id)sender {
   
}


// 正则判断手机号码地址格式
- (BOOL)isMobileNumber:(NSString *)mobileNum {
    
    //    电信号段:133/153/180/181/189/177
    //    联通号段:130/131/132/155/156/185/186/145/176
    //    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虚拟运营商:170
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    return [regextestmobile evaluateWithObject:mobileNum];
}

@end
